﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Tank90
{
    /// <summary>
    ///     类Element。
    /// </summary>
    [Serializable]
    public class ElementMap
    {
        public ElementMap()
        {
        }

        #region Model

        private int? _checkid;
        private string _elementtypevch;
        private int _id;
        private int? _locationx;
        private int? _locationy;

        /// <summary>
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }

        /// <summary>
        /// </summary>
        public string ElementTypeVch
        {
            set { _elementtypevch = value; }
            get { return _elementtypevch; }
        }

        /// <summary>
        /// </summary>
        public int? LocationX
        {
            set { _locationx = value; }
            get { return _locationx; }
        }

        /// <summary>
        /// </summary>
        public int? LocationY
        {
            set { _locationy = value; }
            get { return _locationy; }
        }

        /// <summary>
        /// </summary>
        public int? CheckID
        {
            set { _checkid = value; }
            get { return _checkid; }
        }

        #endregion Model

        #region  Method

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public ElementMap(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ID,ElementTypeVch,LocationX,LocationY,CheckID ");
            strSql.Append(" FROM [Element] ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    this.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ElementTypeVch"] != null)
                {
                    ElementTypeVch = ds.Tables[0].Rows[0]["ElementTypeVch"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LocationX"] != null && ds.Tables[0].Rows[0]["LocationX"].ToString() != "")
                {
                    LocationX = int.Parse(ds.Tables[0].Rows[0]["LocationX"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LocationY"] != null && ds.Tables[0].Rows[0]["LocationY"].ToString() != "")
                {
                    LocationY = int.Parse(ds.Tables[0].Rows[0]["LocationY"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CheckID"] != null && ds.Tables[0].Rows[0]["CheckID"].ToString() != "")
                {
                    CheckID = int.Parse(ds.Tables[0].Rows[0]["CheckID"].ToString());
                }
            }
        }

        /// <summary>
        ///     得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "Element");
        }

        /// <summary>
        ///     是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from [Element]");
            strSql.Append(" where ID=@ID ");

            SqlParameter[] parameters =
            {
                new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        ///     增加一条数据
        /// </summary>
        public void Add()
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into [Element] (");
            strSql.Append("ID,ElementTypeVch,LocationX,LocationY,CheckID)");
            strSql.Append(" values (");
            strSql.Append("@ID,@ElementTypeVch,@LocationX,@LocationY,@CheckID)");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ID", SqlDbType.Int, 4),
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, 50),
                new SqlParameter("@LocationX", SqlDbType.Int, 4),
                new SqlParameter("@LocationY", SqlDbType.Int, 4),
                new SqlParameter("@CheckID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;
            parameters[1].Value = ElementTypeVch;
            parameters[2].Value = LocationX;
            parameters[3].Value = LocationY;
            parameters[4].Value = CheckID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update()
        {
            var strSql = new StringBuilder();
            strSql.Append("update [Element] set ");
            strSql.Append("ElementTypeVch=@ElementTypeVch,");
            strSql.Append("LocationX=@LocationX,");
            strSql.Append("LocationY=@LocationY,");
            strSql.Append("CheckID=@CheckID");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, 50),
                new SqlParameter("@LocationX", SqlDbType.Int, 4),
                new SqlParameter("@LocationY", SqlDbType.Int, 4),
                new SqlParameter("@CheckID", SqlDbType.Int, 4),
                new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ElementTypeVch;
            parameters[1].Value = LocationX;
            parameters[2].Value = LocationY;
            parameters[3].Value = CheckID;
            parameters[4].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from [Element] ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public void GetModel(int ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ID,ElementTypeVch,LocationX,LocationY,CheckID ");
            strSql.Append(" FROM [Element] ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ID", SqlDbType.Int, 4)
            };
            parameters[0].Value = ID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    this.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ElementTypeVch"] != null)
                {
                    ElementTypeVch = ds.Tables[0].Rows[0]["ElementTypeVch"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LocationX"] != null && ds.Tables[0].Rows[0]["LocationX"].ToString() != "")
                {
                    LocationX = int.Parse(ds.Tables[0].Rows[0]["LocationX"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LocationY"] != null && ds.Tables[0].Rows[0]["LocationY"].ToString() != "")
                {
                    LocationY = int.Parse(ds.Tables[0].Rows[0]["LocationY"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CheckID"] != null && ds.Tables[0].Rows[0]["CheckID"].ToString() != "")
                {
                    CheckID = int.Parse(ds.Tables[0].Rows[0]["CheckID"].ToString());
                }
            }
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [Element] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}